package com.hare.harelearn.DataStructure.StrAlgorithm;

import java.util.Arrays;

/**
 * @program: HareLearn
 * @description: 反转字符串
 * @author: Hare
 * @create: 2025−03-07 16:57
 * @Version 1.0
 **/
public class 反转字符串 {
    public static void main(String[] args) {
        reverse(new char[]{'h', 'e', 'l', 'l', 'o','p','u','g','d','5','c'});
        reverseString(new char[]{'h', 'e', 'l', 'l', 'o','p','u','g','d','5','c'});
    }

    /**
     * 反转字符串
     *
     * @param str
     * @return
     */
    public static void reverse(char [] str){
        for (int i = 0; i < str.length / 2; i++) {
            char temp = str[i];
            str[i] = str[str.length - 1 - i];
            str[str.length - 1 - i] = temp;
        }
        System.out.println(Arrays.toString(str));
    }

    /**
     * 双指针法
     * @param s
     */
    public static void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;
        char temp = 0;
        while (left <= right) {
            temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
        System.out.println(Arrays.toString(s));
    }
}
